﻿#region --- License & Copyright Notice ---
/*
ConsoleFx CommandLine Processing Library

Copyright (c) 2006-2012 Jeevan James
All rights reserved.

The contents of this file are made available under the terms of the
Eclipse Public License v1.0 (the "License") which accompanies this
distribution, and is available at the following URL:
http://opensource.org/licenses/eclipse-1.0.txt

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

By using this software in any fashion, you are agreeing to be bound by the
terms of the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.IO;

using ConsoleFx.Programs.Simple;
using ConsoleFx.Validators;

namespace ConsoleFx.Samples.GettingStarted.Simple
{
    internal static class Program
    {
        private static int Main()
        {
            var app = new ConsoleProgram(Handler);
            try
            {
                app.AddOption("verbose", "v")
                    .Flag(() => VerboseOutput);
                app.AddOption("type", "t", expectedParameters: 1)
                    .ValidateWith(new EnumValidator<BackupType> { ErrorMessage = "Invalid backup type. Specify Full or Incremental" })
                    .AssignTo(() => BackupType);
                app.AddOption("exclude", "e", maxOccurences: int.MaxValue, minParameters: 1, maxParameters: int.MaxValue)
                    .ValidateWith(ParameterIndex.All, new RegexValidator(RegexPattern.FileMask))
                    .AddToList(() => Exclusions);
                app.AddArgument()
                    .ValidateWith(new PathValidator(PathType.Directory))
                    .AssignTo(() => BackupDirectory);
                app.AddArgument()
                    .ValidateWith(new PathValidator(checkIfExists: false))
                    .ValidateWith(new CustomValidator(CustomBackupFilePathValidator) {
                        ErrorMessage = "The backup file should have a .ZIP extension"
                    }).AssignTo(() => BackupFilePath);
                app.BeforeError += (sender, args) => Console.ForegroundColor = ConsoleColor.Red;
                app.AfterError += (sender, args) => Console.ResetColor();

                return app.Run();
            }
            catch (Exception ex)
            {
                return app.HandleError(ex);
            }
        }

        private static int Handler()
        {
            Console.WriteLine("Backup {0} to {1}", BackupDirectory, BackupFilePath);
            Console.WriteLine("Options:");
            Console.WriteLine("    Verbose output: {0}", VerboseOutput);
            Console.WriteLine("    Backup type   : {0}", BackupType);
            if (Exclusions.Count > 0)
            {
                Console.WriteLine("    Exclusions:");
                foreach (string exclusion in Exclusions)
                    Console.WriteLine("        {0}", exclusion);
            }
            else
                Console.WriteLine("    No exclusions");
            return 0;
        }

        private static bool CustomBackupFilePathValidator(string arg)
        {
            string extension = Path.GetExtension(arg);
            return extension != null && extension.Equals(".zip", StringComparison.OrdinalIgnoreCase);
        }

#pragma warning disable 649
        private static bool VerboseOutput;
        private static BackupType BackupType;
        private static readonly List<string> Exclusions = new List<string>();
        private static string BackupDirectory;
        private static string BackupFilePath;
#pragma warning restore 649
    }

    public enum BackupType
    {
        Full,
        Incremental,
    }
}